package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.mapper.DCustomerMapper;
import com.igeekhome.pojo.DCustomer;
import com.igeekhome.pojo.DCustomerExample;
import com.igeekhome.service.ICustomerService;
import com.igeekhome.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 立以宁
 */
@Service("customerService")
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private DCustomerMapper dCustomerMapper;




    @Override
    public PageInfo<DCustomer> selectAll(Integer limit, Integer page, DCustomer condition) {

        DCustomerExample dCustomerExample = new DCustomerExample();
        DCustomerExample.Criteria criteria = dCustomerExample.createCriteria();

        criteria.andClientTypeEqualTo(condition.getClientType());

        if (!CommonUtil.isEmpty(condition.getClientName())){
            criteria.andClientNameLike(condition.getClientName());
        }

        if (condition.getClientId() != null){
            criteria.andClientIdEqualTo(condition.getClientId());
        }

        PageHelper.startPage(page,limit);
        List<DCustomer> dCustomers = dCustomerMapper.selectByExample(dCustomerExample);
        PageInfo<DCustomer> pageInfo = new PageInfo<>(dCustomers);

        return pageInfo;
    }

    @Override
    public DCustomer selectById(Integer id) {
        DCustomer dCustomer = dCustomerMapper.selectByPrimaryKey(id);
        return dCustomer;
    }


    @Override
    public void add(DCustomer customer) {
        //用户账户默认为手机号
        customer.setLoginAccount(customer.getTelephone());
        dCustomerMapper.insert(customer);
    }

    @Override
    public void update(DCustomer customer) {
        //手机号可能会被修改，所以账户也要改,那么相应的数据呢？
        customer.setLoginAccount(customer.getTelephone());
        dCustomerMapper.updateByPrimaryKey(customer);
    }


    @Override
    public void delete(Integer clientId, Integer clientType) {
        DCustomerExample dCustomerExample = new DCustomerExample();
        DCustomerExample.Criteria criteria = dCustomerExample.createCriteria();
        criteria.andClientTypeEqualTo(clientType).andClientIdEqualTo(clientId);
        dCustomerMapper.deleteByExample(dCustomerExample);
    }

    @Override
    public List<DCustomer> selectToList() {
        DCustomerExample example = new DCustomerExample();
        example.createCriteria().andClientTypeEqualTo(1);
        return dCustomerMapper.selectByExample(example);
    }
}
